﻿using Dapper;
using System;
using BCEntity.Common.Cooperation.WebsiteCooperationFollowUp;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Cooperation.WebsiteCooperationFollowUp
{
    /// <summary>
    /// 官网合作留言跟进记录
    /// </summary>
    public class WebsiteCooperationFollowUpData : IWebsiteCooperationFollowUpData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitecooperationfollowup";
        private const string QUERY = @"SELECT WebsiteCooperationFollowUpId,WebsiteCooperationId,Company,Content,CreateAdminId,CreateAdminName,CreateTime FROM websitecooperationfollowup";
        public WebsiteCooperationFollowUpData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网合作留言跟进记录是否存在
        public async Task<bool> Exists(int websiteCooperationFollowUpId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCooperationFollowUpId=?WebsiteCooperationFollowUpId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationFollowUpId", websiteCooperationFollowUpId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网合作留言跟进记录
        public async Task<WebsiteCooperationFollowUpEntity> Add(WebsiteCooperationFollowUpEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitecooperationfollowup", new string[] { "WebsiteCooperationId", "Company", "Content", "CreateAdminId", "CreateAdminName", });
            sql = string.Format("{0} {1} WHERE WebsiteCooperationFollowUpId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationId", entity.WebsiteCooperationId, DbType.Int32, size: 11);
            parameters.Add("?Company", entity.Company?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Content", entity.Content?.Trim(), DbType.String);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCooperationFollowUpEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion
        
        #region 删除官网合作留言跟进记录
        public async Task<bool> Delete(int websiteCooperationFollowUpId)
        {
            string sql = @"DELETE FROM websitecooperationfollowup WHERE WebsiteCooperationFollowUpId=?WebsiteCooperationFollowUpId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationFollowUpId", websiteCooperationFollowUpId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网合作留言跟进记录
        public async Task<WebsiteCooperationFollowUpEntity> GetEntity(int websiteCooperationFollowUpId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCooperationFollowUpId=?WebsiteCooperationFollowUpId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationFollowUpId", websiteCooperationFollowUpId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteCooperationFollowUpEntity>(sql, parameters);
        }
        #endregion
        
        #region 获取官网合作留言跟进记录分页
        public async Task<(IEnumerable<WebsiteCooperationFollowUpEntity>, int)> Query(int? websiteCooperationId, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (websiteCooperationId.HasValue)
            {
                wheres.Add("WebsiteCooperationId=?WebsiteCooperationId");
                parameters.Add("?WebsiteCooperationId", websiteCooperationId, DbType.Int32, size: 11);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitecooperationfollowup {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteCooperationFollowUpEntity, int>(sql, parameters);
        }
        #endregion
    }
}



